package com.gljx.web.controller.weixin;

import com.gljx.constant.Constant;
import com.gljx.util.Calculate;
import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Clazz;
import com.gljx.web.Entity.Person;
import com.gljx.web.Entity.Student;
import com.gljx.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/weixin/zxbm")
public class ZxbmController {

	@Autowired
	private ClazzService clazzService;

	@Autowired
	private PersonService personService;

	@Autowired
	private CustomLogService customLogService;

	@Autowired
    private StudentService studentService;

	@Autowired
	private WeixinZxbmService zxbmService;

	@RequestMapping("/look")
	public ModelAndView look(HttpServletRequest request, String classType){
		//无校区 或校区不符合规范时修改
		if(classType == null ||
			(!classType.equals(Constant.School_Key_ChangNan)
					&&!classType.equals(Constant.School_Key_ChangBei))){
			classType = Constant.School_Key_ChangBei;//默认昌南
		}
		String company;
		if(Constant.School_Key_ChangBei.equals(classType)){
			company = Constant.School_Key_ChangBei_ID;
		}else{
			company = Constant.School_Key_ChangNan_ID;
		}
		ModelAndView mav=new ModelAndView();
		mav.getModel().put("clames",clazzService.selectByCompanyID(company));
		mav.getModel().put("schoolName",classType);
		mav.setViewName("weixin/zxbm/add");
		return mav;
	}

	/**
	 * 推荐人列表
	* @param request
	* @param response
	* @author LiXiang  @date 2018年1月8日 下午3:42:55
	 */
	@RequestMapping("/tjrList")
	@ResponseBody
	public List<Person> smZf(HttpServletRequest request,
                             HttpServletResponse response,
                             String name,
                             String company,
                             int pageSize,
                             int pageNum){
		if(Constant.School_Key_ChangBei.equals(company)){
			company = Constant.School_Key_ChangBei_ID;
		}else{
			company = Constant.School_Key_ChangNan_ID;
		}
		//System.out.println(name+"---");
		List<Person> a = personService.selectWeiXinTjrList(company, name, pageSize, pageNum);
		return a;
	}
	/**
	 * 立即报名  直接跳转至报名页面
	* @param request
	* @param response
	* @param sf
	* @throws IOException
	* @author LiXiang  @date 2018年1月8日 下午2:54:49
	 */
	@RequestMapping("/ljbm")
	@ResponseBody
	public Student smZf(HttpServletRequest request,
                        HttpServletResponse response,
                        Student student, String xq, BigDecimal sf) throws IOException {
		if(Constant.School_Key_ChangBei.equals(xq)){
			xq = Constant.School_Key_ChangBei_ID;
		}else{
			xq = Constant.School_Key_ChangNan_ID;
		}
		//获取当前时间
        String createTime = TimeFormat.timeFormat(new Date());
        //利用UUID生成fid
        student.setFid(GetFid.UUIDIntoFid());
        student.setCfsex(1L);//男
        //控制单元id
        student.setFcontrolunitid(xq);
        //公司id
        student.setCfficompanyid(xq);
        //当前时间
        student.setFcreatetime(createTime);
        //入学时间
        student.setCfrxsj(createTime);
        //fnumber
        /*String fnumber = customLogService.selectFunmber("CT_DS_STUDENT",xq);*/
        student.setFnumber(GetFnumber.fnumbers("X"));
        //学员状态
        student.setCfstudentstate(1L);
        //将班级的数据存入
        Clazz clazz = clazzService.selClazzById(student.getCfclazzid());
        //申领类型   默认 初次申领
        student.setCfsllx(1L);
        //金额
        student.setCfamount(clazz.getCfprice());
        //现准架车型代号
        //student.setCfxzjcxdh(clazz.getCfsycl());
        //申请准驾车型代号
        student.setCfslzjcxdh(clazz.getCfsycl());

        //微信订单号
        student.setDdno(Calculate.getDdGl());
        //计算欠费
        BigDecimal yf = student.getCfamount();
		student.setCfarrearage(yf);
		//默认本校
		student.setCfbxwx("1");
		//插入并书写日志
        studentService.insertStudent(student);
		customLogService.addLog("学员入学登记", createTime + "办理了入学登记", student.getFid(), "",xq);

		//设置学生应付方便传入前台
        student.setCfreceived(sf);

        return student;
	}

	@RequestMapping(value="/wxDdSuccess")
	public ModelAndView wxDdSuccess(HttpServletRequest request, String ddno,//订单号
                                    String dwid,
									String sf,//支付费用
                                    String jf,
                                    String dzid,
                                    String wlx,
                                    String tradno,//微信的商家订单号
                                    String dwlx//公司
															){
		ModelAndView mav=new ModelAndView();
		System.out.println("sf:"+sf);
		System.out.println("ddh:"+ddno);
		mav.setViewName("weixin/zfcg");
		if("gljx".equals(dwlx) ){
			mav.getModel().put("dwlx",dwlx);//判别公司
			mav.getModel().put("sf",sf);//实付
			try {

				//学员
				Student student = studentService.selectStudentByDDNO(ddno);

				System.out.println("xs:"+student);
				System.out.println("bj:"+student.getCfclazzName());
				String paNumber = GetFnumber.fnumbers("YW");
				zxbmService.setPxsf(student, student.getCfficompanyid(), new BigDecimal(sf), paNumber);
				customLogService.addLog("财务管理", TimeFormat.timeFormat(new Date()) + "培训收费，学员微信报名", student.getFid(), "",student.getCfficompanyid());
				mav.getModel().put("ddno",student.getDdno());//订单
				mav.getModel().put("yf",student.getCfamount());//应付
				String xq = "";
				if(Constant.School_Key_ChangBei_ID.equals(student.getCfficompanyid())){
					xq = Constant.School_Key_ChangBei;
				}else if (Constant.School_Key_ChangNan_ID.equals(student.getCfficompanyid())){
					xq = Constant.School_Key_ChangNan;
				}
				mav.getModel().put("xq",xq);//校区
				mav.getModel().put("kc",student.getCfclazzName());//课程
				mav.getModel().put("falg","success");//判别
			} catch (Exception e) {
				e.printStackTrace();
				mav.getModel().put("ddno",ddno);//订单
				mav.getModel().put("yf","");//应付
				mav.getModel().put("xq","");//校区
				mav.getModel().put("kc","");//课程
				mav.getModel().put("falg","error");//判别
			}

		}

		return mav;
	}

}
